<?php

namespace App\Admin\Controllers\Configs;

use App\Admin\Repositories\PayConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class PayConfigController extends AdminController
{
    /*
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        session(['pay_method_id' => request()->pay_method_id]);
        return Grid::make(new PayConfig(), function (Grid $grid) {
            $grid->model()->where('pay_method_id',request()->pay_method_id??session('pay_method_id'));
            $grid->column('id')->sortable();
            $grid->column('pay_method_id');
            $grid->column('domain')->editable();
            $grid->column('appid')->editable();
            $grid->column('appsecret')->editable();
            $grid->column('mchid')->editable();
            $grid->column('key')->editable();
            $grid->column('state')->switch();
        
            $grid->enableDialogCreate();

            $grid->disableFilterButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PayConfig(), function (Show $show) {
            $show->field('id');
            $show->field('pay_method_id');
            $show->field('domain');
            $show->field('appid');
            $show->field('appsecret');
            $show->field('mchid');
            $show->field('key');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PayConfig(), function (Form $form) {
            $form->display('id');
            $form->hidden('pay_method_id')->default(session('pay_method_id'));
            $form->text('domain');
            $form->text('appid');
            $form->text('appsecret');
            $form->text('mchid');
            $form->text('key');
            $form->switch('state','是否启用')->default(1);
        
            $form->disableHeader();
            
            $form->display('created_at');
            $form->display('updated_at');

            //编辑缓存同步
            $form->saved(function(Form $form){
                cache()->forget('pay_method');
            });

            $form->deleted(function (Form $form, $result) {
                // 获取待删除行数据，这里获取的是一个二维数组
                $data = $form->model()->toArray();
                
                // 通过 $result 可以判断数据是否删除成功
                if ( $result) {
                    cache()->forget('pay_method');
                    // 返回删除成功提醒，此处跳转参数无效
                    return $form->response()->success('删除成功');
                }
                return $form->response()->error('数据删除失败');
            });
        });
    }
}
